package com.ctn.bootstarp.tags.util;

import tldgen.Function;

/**
 * @author weiqi
 * @date 2013年8月5日
 */
public class TagUtil{
	
	public static enum Type{
		DEFAULT(""),
		INFO("alert-info"),
		ERROR("alert-error"),
		SUCCESS("alert-success"),
		BLOCK("alert-block");
		
		private final String type;
		
		private Type(String type){
			this.type = type;
		}
		
		@Override
		public String toString() {
			return this.type;
		}
	}
	
	public static class MsgType{
		/**是否块级元素**/
		private boolean blockEle;
		private boolean closeable;
		private Type type;
		/**是否具有alert-block样式**/
		private boolean block = false;
		
		public void setBlock(boolean block) {
			this.block = block;
		}

		public boolean isBlockEle() {
			return blockEle;
		}

		public boolean isCloseable() {
			return closeable;
		}

		public Type getType() {
			return type;
		}

		MsgType(Type type,boolean blockEle,boolean closeable){
			this.type = type;
			this.blockEle = blockEle;
			this.closeable = closeable;
		}
		
		public StringBuffer getMarks() {
			StringBuffer res = new StringBuffer();
			if(blockEle){
				res.append("<div class='alert");
			}else{
				res.append("<span class='alert");
			}
			res.append(" ").append(type);
			if(block){
				res.append(" alert-block");
			}
			res.append("'>");
			
			if(closeable){
				res.append("<button class=\"close\" type=\"button\" data-dismiss=\"alert\">&times;</button>");
			}
			return res;
		}
	}
	
	
	public static final String div_end = "</div>";
	public static final String span_end = "</span>";

	
	private static Type getMsgType(Object type){
		Type msgType = Type.DEFAULT;
		if(type instanceof String){
			if(type.equals("info")){
				msgType = Type.INFO;
			}
			if(type.equals("error")){
				msgType = Type.ERROR;
			}
			if(type.equals("success")){
				msgType = Type.SUCCESS;
			}
			if(type.equals("block")){
				msgType = Type.BLOCK;
			}
		}
		return msgType;
	}
	
	@Function
	public static String printMessageEx(String message,Object type,boolean block,boolean blockEle,boolean closeable){
		Type resolveType = getMsgType(type);
		if(type instanceof Type){
			resolveType = (Type) type;
		}
		MsgType msgType = new MsgType(resolveType, blockEle, closeable);
		if(block){
			msgType.setBlock(true);
		}
		StringBuffer s = msgType.getMarks();
		s.append(message);
		if(blockEle){
			s.append(div_end);
		}else{
			s.append(span_end);
		}
		return s.toString();
	}
	
	@Function
	public static String printMessage(String message,Object type,boolean blockEle,boolean closeable){
		return printMessageEx(message, type, false, blockEle, closeable);
	}
	
	@Function
	public static String printAlert(String message){
		return printMessage(message,Type.DEFAULT,false,false);
	}
	
	@Function(description="what the fuck error1!")
	public static String printError(String message){
		return printMessage(message,Type.ERROR,false,false);
	}
	
	@Function(description="what the fuck error2!")
	public static String printErrorExtra(String message,boolean block,boolean closeable){
		return printMessage(message,Type.ERROR,block,closeable);
	}
	
	@Function
	public static String printInfo(String message){
		return printMessage(message,Type.INFO,false,false);
	}
	
	@Function
	public static String printSuccess(String message){
		return printMessage(message,Type.SUCCESS,false,false);
	}
	
	@Function
	public static String printBlock(String message,String type){
		return printMessageEx(message, type, true, false, false);
	}
	
	@Function
	public static String concat(Object obj1,Object obj2){
		return obj1.toString().concat(obj2.toString());
	}
	
	@Function
	public static String concat2(Object obj1,Object obj2,Object obj3){
		return obj1.toString().concat(obj2.toString()).concat(obj3.toString());
	}
	
}
